const Sort = require('../Sort');

module.exports = class BubbleSort {
  sort(oringinalArray) {
    let swapped = false;

    const array = [...oringinalArray];

    for (let i = 0; i < array.length - 1; i++) {
      swapped = false;
      this.callbacks.visitingCallback(array[i]);
       
      for (let j = 0; j < array.length - i - 1; j++) {
        if (array[j] > array[j + 1]) {
          [array[j], array[j + 1]] = [array[j + 1], array[j]];
          swapped = true;
        }
      }

      if (!swapped) {
        return array;
      }
    }

    return array;
  } 
}